package com.khh.controller;

import com.khh.entity.User;
import com.khh.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Random;

/**
 * Created by webrx on 2017-09-19.
 */

@Slf4j
//@RestController
@RequestMapping(value = "/user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;


    /***********忘记密码**************/
    //忘记密码界面
    @RequestMapping("/findpwd")
    public String findpwd(){
        return "forgetPwd";
    }



    /************* 登录  *************/
    //登录界面
    @RequestMapping("/login")
    public String login(){
        return "login";
    }


    //登录验证
    @RequestMapping("/logincheck")
    public String logincheck(String email, String password, HttpSession session, HttpServletRequest request){

        User user = new User();
        user.setuPhone(email);
        user.setuPassword(password);

        User userinfo = userService.selectOne(user);
        if (userinfo != null){
            session.setAttribute("userinfo",userinfo);
            return "index";
        } else {
            System.out.println(999);
            request.setAttribute("loginerror","登录失败，请重试");
            return "login";
        }

    }



    /************* 注册 *************/

    //注册界面
    @RequestMapping("/register")
    public String show(Model model){
        return "register";
    }

    //注册插入数据库
    @RequestMapping("/insert")
    public String insert(Model model, @RequestParam("mobile") String phone, @RequestParam("email") String email, @RequestParam("password") String password){
        User u = new User();
        u.setuPhone(phone);
        u.setuPassword(password);
        int n = userService.insert(u);
        if (n > 0){
            return "redirect:/user/login";
        } else {
            model.addAttribute("registererror","注册失败");
            return "redirect:/user/register";
        }

    }



    /***************** 校验 **********************/
    //验证phone唯一性
    @RequestMapping("/checkphone") @ResponseBody
    public int checkphone(String phone){
        User u = new User();
        u.setuPhone(phone);
        User user = userService.selectOne(u);
        if (user != null){
            return 1;
        }else {
            return 0;
        }
    }
    //验证码检验
    @RequestMapping("/checkout") @ResponseBody
    public int check(String maV,HttpSession session){

        int n = 0;
        String s = session.getAttribute("checkM").toString();
        if (maV.equalsIgnoreCase(s)){
            n = 1;
        }
        return n;
    }

}
